package top.oylan.manager.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Types;
import top.oylan.manager.service.TypeService;
import top.oylan.manager.tools.Result;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-05-26
 */
@Controller
@RequestMapping("/admin/type")
public class AdminTypeController {

    @Resource
    TypeService service;

    @RequestMapping("/list")
    public String index(ModelMap modelMap){
        List<Types> list =  service.selectAll();
        modelMap.addAttribute("list",list);
        return "admin/type/list";
    }

    @RequestMapping("/add")
    public String add(){

        return "admin/type/add";
    }

    @RequestMapping("/edit")
    public String edit(ModelMap modelMap, @RequestParam Integer id){
        Types types = service.findById(id);
        modelMap.addAttribute("types",types);
        return "admin/type/edit";
    }

    //  保存数据
    @RequestMapping("/save")
    @ResponseBody
    public Result saveOrUpdate(@RequestBody Types types){
        Result result = new Result();
        if(null != types.getId() && types.getId() != 0){
            //修改
            service.updateTypes(types);
        }else{
            //添加
            service.insertTypes(types);
        }
        result.setCode((byte) 1);
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 删除
     */

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestParam("id")Integer id){
        Result result = new Result();
        service.deleteTypes(id);
        result.setCode((byte) 1);
        result.setMessage("删除成功");
        return result;
    }
}
